/*
 * @(#)DmsArchiveSchedultJob.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.dms.core;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.dcivision.dms.bean.DmsArchive;
import com.dcivision.dms.bean.DmsValidation;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.user.bean.UserRecord;

 /**
  DmsArchiveSchedultJob.java
  This class is automatically to archive documents.
    @author            Jenny Li
    @company           DCIVision Limited
    @creation date     08/09/2005
    @version           $Revision: 1.11.2.1 $
 */
public class DmsArchiveSchedulerJob implements Job {

  public static final String REVISION = "$Revision: 1.11.2.1 $";
  
  protected Log log = LogFactory.getLog(this.getClass().getName());
  SessionContainer sessionContainer;
  Connection conn = null;
  
  /** Creates a new instance of SchedulerTest */
  public DmsArchiveSchedulerJob() {
    
  }
  
  public void execute(JobExecutionContext context) throws JobExecutionException{
    Integer userRecordID=null;
    
    try{   
      sessionContainer = new SessionContainer();
      conn = DataSourceFactory.getConnection();
      DmsArchiveManager dmsArchiveManager = new DmsArchiveManager(sessionContainer,conn);
      
      JobDataMap dataMap = context.getJobDetail().getJobDataMap();
      String archiveName=dataMap.getString("archiveName");
      String archiveType=dataMap.getString("archiveType");
      String destinationID=dataMap.getString("destinationID");
      String rootID=dataMap.getString("rootID");
      String startDate=dataMap.getString("startDate");
      String endDate=dataMap.getString("endDate");
      List userDefinedFieldIDList=(List)dataMap.get("userDefinedFieldIDList");
      ArrayList clipboardList=(ArrayList)dataMap.get("clipboardList");
      List defaultProfileSettingList=(List)dataMap.get("defaultProfileSettingList");
      String offlineViewPath=dataMap.getString("offlineViewPath");
      userRecordID=TextUtility.parseIntegerObj(dataMap.getString("userRecordID"));
      
      //EIP-1405 LEE 07/01/04 get action user/ip and set to sessionContainer
      UserRecord userReocrd = (UserRecord)dataMap.get("userRecord");
      String userip = dataMap.getString("userIPAddress");
      sessionContainer.setUserRecord(userReocrd);
      sessionContainer.setUserIPAddress(userip);
      
      String[] userDefinedFieldID=null;
      if(!Utility.isEmpty(userDefinedFieldIDList)){
        userDefinedFieldID=new String[userDefinedFieldIDList.size()];
        for(int i=0;i<userDefinedFieldIDList.size();i++){
            userDefinedFieldID[i]=(String)userDefinedFieldIDList.get(i);
        }
      }
      
      //DmsDocumentDAObject dmsDocumentDAObject = new DmsDocumentDAObject(sessionContainer,conn);
      String[] destination = TextUtility.splitString(destinationID,",");
      
      DmsArchive dmsArchive=new DmsArchive();
      dmsArchive.setArchiveName(archiveName);
      dmsArchive.setArchiveType(archiveType);
      dmsArchive.setArchiveDateFrom(parseTimestamp(startDate));
      dmsArchive.setArchiveDateTo(parseTimestamp(endDate));
      dmsArchive.setRootID(TextUtility.parseIntegerObj(rootID));
      dmsArchive.setRecordStatus(GlobalConstant.RECORD_STATUS_ACTIVE);
      dmsArchive.setCreatorID(userRecordID);
      dmsArchive.setUpdaterID(userRecordID);
      
      DmsValidation validation = dmsArchiveManager.validateArchiveAction(dmsArchive, destination, startDate, endDate, userDefinedFieldID, true);
      if (validation.isSuccess()) {
        dmsArchiveManager.archiveDocument(dmsArchive, validation, destination, userDefinedFieldID, offlineViewPath, defaultProfileSettingList, null);
      }
    }catch(Exception e){
      log.error(e, e);
    }finally {
      try {conn.close();}catch(Exception ex) {}finally {conn=null;}
    }
  }
  
  public Timestamp parseTimestamp(String inStr) {
    if (com.dcivision.framework.Utility.isEmpty(inStr)) {
      return(null);
    }
    String dataFormat = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT);
    java.util.Date tmpDate = null;
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(dataFormat, new java.util.Locale("en", "US"));
    try {
      tmpDate = sdf.parse(inStr);
      return (new Timestamp(tmpDate.getTime()));
    } catch (java.text.ParseException pe) {
      log.error("Error when parsing a datetime string.", pe);
      return(null);
    }
  }
}
